package org.common.eureka.controller;

import org.common.eureka.service.IUserFeignClient;
import org.sam.commons.entity.pojo.SecUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ArchivesController {
    private final Logger logger = LoggerFactory.getLogger(ArchivesController.class);

    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    private IUserFeignClient userFeignClient;

    @GetMapping("/user/{id}")
    public SecUser findById(@PathVariable String id) {
        //用Feign声明式方式请求
        SecUser user = this.userFeignClient.queryOne(id);

        return user;
    }

    @GetMapping("/user/{userName}")
    public String findByName(@PathVariable String userName) {
        //用Feign声明式方式请求
        return this.userFeignClient.queryJsonByName(userName);
    }

    /**
     * 打印负载均衡日志
     */
    @GetMapping("/log-instance")
    public void logArchiverInstance() {
        ServiceInstance serviceInstance = this.loadBalancerClient.choose("EurekaUserSystem");
        //打印当前选择的是哪个节点
        logger.info("{}:{}:{}", serviceInstance.getServiceId(), serviceInstance.getHost(), serviceInstance.getPort());
    }
}
